import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class TreeNode{
    char value;
    TreeNode left;
    TreeNode right;

    public TreeNode(char value) {
        this.value = value;
    }
}
public class TreeTest {
    public void levelOrder1(TreeNode root){
        if (root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.println(cur.value);
            if (cur.left!=null){
                queue.offer(cur.left);
            }
            if (cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }

    public List<List<Character>> levelOrder2(TreeNode root) {
        if (root == null){
            return null;
        }
        List<List<Character>> retList = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while(size != 0){
                TreeNode cur = queue.poll();
                list.add(cur.value);

                if (cur.left != null){
                    queue.offer(cur.left);
                }
                if (cur.right !=null){
                    queue.offer(cur.right);
                }

                size--;
            }
            retList.add(list);
        }
        return retList;
    }

    public boolean isCompleteTree(TreeNode root){
        if (root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
//                跳出循环之后，要遍历剩下元素是不是都是空
            }
        }
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur != null){
                return false;
            }
        }
        return true;
    }
}
